package com.mindshifter.pivotaltracker.services.v3.core
{
	import com.mindshifter.pivotaltracker.services.v3.events.TrackerEvent;
	import com.mindshifter.pivotaltracker.services.v3.events.TrackerServiceEvent;
	import com.mindshifter.pivotaltracker.services.v3.interfaces.ITracker;
	import com.mindshifter.pivotaltracker.services.v3.service.TrackerService;
	
	import flash.display.Sprite;
	import flash.net.URLRequestHeader;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	
	import org.spicefactory.lib.reflect.types.Void;

	/**
	 * 
	 * @author Mindshifter
	 * 
	 */
	public class Tracker extends Sprite implements ITracker
	{	
		private var _projects:Vector.<Project>;
		private var _token:Token;
		private var _user:TrackerUser;
		private var _service:TrackerService;
		private var _serviceURLVariables:URLVariables;
		private var _serviceHeaders:URLRequestHeader;
		
		public function Tracker(user:TrackerUser) {
			this._user = user;			
			this._service = new TrackerService();
			this._serviceURLVariables = new URLVariables();
			this._serviceHeaders = new URLRequestHeader();
		}
		public function login():void
		{
			_serviceURLVariables.username= _user.username;
			_serviceURLVariables.password= _user.password;
			_serviceHeaders.name = "X-TrackerToken";
			_serviceHeaders.value = _user.token.guid;
			_service.connect(TrackerURLBuilder.getTokenUrl(),_serviceURLVariables,_serviceHeaders);
			_service.addEventListener(TrackerServiceEvent.SERVICE_CONNECTED,serviceConnectedHandler);
			_service.addEventListener(TrackerServiceEvent.SERVICE_CONNECTION_ERROR,serviceErrorHandler);
			
		}
		
		private function serviceConnectedHandler(event:TrackerServiceEvent):void
		{	
			_service.removeEventListener(TrackerServiceEvent.SERVICE_CONNECTED,serviceConnectedHandler);
			_service.removeEventListener(TrackerServiceEvent.SERVICE_CONNECTION_ERROR,serviceErrorHandler);	
			trace(event.serviceData);
			dispatchEvent(new TrackerEvent(TrackerEvent.TRACKER_USER_LOGGED_IN,event.serviceData));
		}
		private function serviceErrorHandler(event:TrackerServiceEvent):void
		{
			_service.removeEventListener(TrackerServiceEvent.SERVICE_CONNECTED,serviceConnectedHandler);
			_service.removeEventListener(TrackerServiceEvent.SERVICE_CONNECTION_ERROR,serviceErrorHandler);	
			trace(event.type);
			dispatchEvent(new TrackerEvent(TrackerEvent.TRACKER_USER_NOT_EXISTS));
		}
		public function getProjects():void
		{	
			_serviceHeaders.name = "X-TrackerToken";
			_serviceHeaders.value = _user.token.guid;
			_service.connect(TrackerURLBuilder.getAllProjectsUrl(),null,_serviceHeaders,URLRequestMethod.POST);
			_service.addEventListener(TrackerServiceEvent.SERVICE_CONNECTED,serviceConnectedHandler);
			_service.addEventListener(TrackerServiceEvent.SERVICE_CONNECTION_ERROR,serviceErrorHandler);			
		}	
		public function get projects():Vector.<Project>
		{
			return _projects;
		}	
		public function get token():Token
		{
			return _token;
		}	
	}
}